<?php
/**
 * Created by PhpStorm.
 * User: Vlad
 * Date: 1/11/14
 * Time: 4:51 PM
 */

class Statistics {

    public static function UpdateStat($statType, $incrementValue)
    {
        DB::Query(" update
                                Statistics
                    set
                                StatValue = StatValue + $incrementValue
                    where
                                StatType = $statType");
    }

    public static function GetStat($statType)
    {
        $stmt = DB::Query(" select
                                        StatValue,
                                        StatInterpretationType
                            from
                                        Statistics
                            where
                                        StatType = $statType");


        $row = $stmt->fetch_assoc();
        $statName = StatType::GetNameFromType($statType);
        $statValue = $row['StatValue'];
        $statInterpretationType = $row['StatInterpretationType'];

        $stmt->close();

        $ret = array (
            'StatName'                  => $statName,
            'StatValue'                 => $statValue,
            'StatInterpretationType'    => $statInterpretationType
        );

        return $ret;
    }

    public static function RecomputeKnowledgeFactor()
    {
        $numObjects = Statistics::GetStat(StatType::NumberOfObjects);
        $numQuestions = Statistics::GetStat(StatType::NumberOfQuestions);
        $value = 100.0 * Queries::GetObjectsQuestionsCount() / ($numObjects['StatValue'] * $numQuestions['StatValue']);
        $statType = StatType::KnowledgeFactor;

        DB::Query(" update
                                Statistics
                    set
                                StatValue = $value
                    where
                                StatType = $statType");
    }

    public static function GetGamesInProgress()
    {
        $stmt = DB::Query(" select
                                        count(*) as NumInProgress
                            from
                                        Games
                            where
                                        PlayedObject is null");

        $row = $stmt->fetch_assoc();

        $stmt->close();

        return $row['NumInProgress'];
    }

    public static function GetAllStats()
    {
        $stmt = DB::Query("select * from Statistics order by StatID");

        while ($row = $stmt->fetch_assoc())
        {
            $statName = StatType::GetNameFromType($row['StatType']);
            $statValue = $row['StatValue'];
            $statInterpretationType = $row['StatInterpretationType'];
            $ret[] = array (
                'StatName'                  => $statName,
                'StatValue'                 => $statValue,
                'StatInterpretationType'    => $statInterpretationType
            );
        }

        $stmt->close();

        return $ret;
    }
} 